package ualrcdg.shared.requests;

import java.util.HashMap;

import ualrcdg.shared.exceptions.CDGParseException;

/**
 * Simple implementation of the "Validate Rule Request" object. See CDG XML Specifications: Server Requests: Validate Rule.
 */
public class ValidateRuleRequest extends CDGRequest
{
	public static final String requestTag = "validate";
	
	public static final String dtdName = "gameOperations.dtd";
	
	public final String type;
	public 	final HashMap< String, String > parameters;
	
	//LATER change type of data member
	public final String data;
	
	/**
	 * Primary constructor
	 *
	 * @param requestId the requestId of the ValidateRuleRequest object to be created
	 * @param type the type of the ValidateRuleRequest object to be created
	 * @param parameters the parameters of the ValidateRuleRequest object to be created
	 * @param data the data of the ValidateRuleRequest object to be created
	 */
	public ValidateRuleRequest( String requestId, String type, String parameters, String data ) throws CDGParseException
	{
		super( requestId );
		
		if( type == null || parameters == null || data == null )
		{
			throw new NullPointerException( "ValidateRuleRequest: unable to create request. Null parameters not allowed." );
		}
		
		this.type = type;
		
		String[] params = parameters.split( ";" );
		
		if( params.length < 1 )
		{
			throw new CDGParseException( "ValidateRuleRequest: unable to create request. Need at least one parameter." );
		}
		
		this.parameters = new HashMap< String, String >( );
		
		for( String s : params )
		{
			String[] param = s.split( ":" );
			if( param.length != 2 )
			{
				throw new CDGParseException( "ValidateRuleRequest: unable to create request. Parameter [" + s + "] is malformed." );
			}
			this.parameters.put( param[0], param[1] );
		}
		
		this.data = data;
	}

	/*
	 * @see ualrcdg.shared.requests.CDGRequest#toString()
	 */
	@Override
	public String toString( )
	{
		return "ValidateRuleRequest [<" + super.toString( ) + "> type=" + type + ", parameters=" + parameters + ", data=" + data + "]";
	}
}
